package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.ListTile;
import javafx.scene.Node;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;
import xworker.lang.util.UtilAction;

import java.util.Collection;

public class ListTileActions {
    static{
        PropertyFactory.regist(ListTile.class, "primaryGraphicProperty", o -> {
            ListTile obj = (ListTile) o;
            return obj.primaryGraphicProperty();
        });
        PropertyFactory.regist(ListTile.class, "secondaryGraphicProperty", o -> {
            ListTile obj = (ListTile) o;
            return obj.secondaryGraphicProperty();
        });
        PropertyFactory.regist(ListTile.class, "wrapTextProperty", o -> {
            ListTile obj = (ListTile) o;
            return obj.wrapTextProperty();
        });
    }

    public static void init(ListTile node, Thing thing, ActionContext actionContext) {
        //在这里添加父Actions的init，如ControlActions.init(node, thing, actionContext);

        Node primaryGraphic = JavaFXUtils.getObject(thing, "primaryGraphic", actionContext);
        if (primaryGraphic != null) {
            node.setPrimaryGraphic(primaryGraphic);
        }

        Node secondaryGraphic = JavaFXUtils.getObject(thing, "secondaryGraphic", actionContext);
        if (secondaryGraphic != null) {
            node.setSecondaryGraphic(secondaryGraphic);
        }

        Collection<String> text = JavaFXUtils.getObject(thing, "text", actionContext);
        if (text != null) {
            node.textProperty().addAll(text);
        }

        if (thing.valueExists("wrapText")) {
            node.setWrapText(thing.getBoolean("wrapText"));
        }

    }

    public static ListTile create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        ListTile node = new ListTile();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createPrimaryGraphic(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ListTile parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setPrimaryGraphic((Node) obj);
                break;
            }
        }
    }

    public static void createSecondaryGraphic(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ListTile parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setSecondaryGraphic((Node) obj);
                break;
            }
        }
    }

    public static void createText(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ListTile parent = actionContext.getObject("parent");

        Collection<String> text = UtilAction.runChildsAsAction(self, actionContext, "parent", parent);
        if(text != null){
            parent.textProperty().addAll(text);
        }
    }

}